﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_ShopRevenueDetail_List2 : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        ReadAction();

        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            string OrderNo = dr["OrderNo"].ToString();
            string CustomerName = dr["CustomerName"].ToString();

            LinkButton lbOrderNo = (LinkButton)e.Item.Cells[1].FindControl("lbOrderNo");                //单据编号
            lbOrderNo.Text = OrderNo;

            LinkButton lbCustomerName = (LinkButton)e.Item.Cells[2].FindControl("lbCustomerName");      //消费者
            lbCustomerName.Text = CustomerName;
        }
    }

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string CustomerID = e.Item.Cells[12].Text;

        if (e.CommandName == "OrderNo")             //单据编号 
        {
            Response.Write("<script> parent.parent.AddTab('ShoppingOrder_Search','单据查询','page/ShoppingOrder_Search.aspx');</script>");
        }
        else if (e.CommandName == "CustomerName")   //消费者信息
        {
            Response.Write("<script> parent.parent.AddTab('CustomerInfo','会员信息','page/CustomerInfo.aspx?id=" + CustomerID + "');</script>");
        }
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        double Cash = 0;            //现金总额
        double CreditCard = 0;      //信用卡总额
        double WebPay = 0;          //网上支付
        double TokenD = 0;          //代用券总额
        double TokenZ = 0;          //折旧券总额
        double Rate = 0;            //税后总额
        double SumVip = 0;          //应收VIP总额
        double SumBV = 0;           //BV总积分

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Cash += Shove._Convert.StrToDouble(dt.Rows[i]["Cash"].ToString(), 0);
                CreditCard += Shove._Convert.StrToDouble(dt.Rows[i]["CreditCard"].ToString(), 0);
                WebPay += Shove._Convert.StrToDouble(dt.Rows[i]["WebPay"].ToString(), 0);
                TokenD += Shove._Convert.StrToDouble(dt.Rows[i]["TokenD"].ToString(), 0);
                TokenZ += Shove._Convert.StrToDouble(dt.Rows[i]["TokenZ"].ToString(), 0);
                Rate += Shove._Convert.StrToDouble(dt.Rows[i]["Rate"].ToString(), 0);
                SumVip += Shove._Convert.StrToDouble(dt.Rows[i]["Vip"].ToString(), 0);
                SumBV += Shove._Convert.StrToDouble(dt.Rows[i]["BV"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + Cash + "</td>");
            output.Write("<td align='center'>" + CreditCard + "</td>");
            output.Write("<td align='center'>" + WebPay + "</td>");
            output.Write("<td align='center'>" + TokenD + "</td>");
            output.Write("<td align='center'>" + TokenZ + "</td>");
            output.Write("<td align='center'>" + Rate + "</td>");
            output.Write("<td align='center'>" + SumBV + "</td>");
            output.Write("<td align='center'>" + SumVip + "</td>");
            output.Write("<td align='center'></td>");
        }
    }

    /// <summary>
    /// 获取上个页面传过来的值(string ShopID,string Day)
    /// </summary>
    private void ReadAction()
    {
        hShopID.Value = Shove._Web.Utility.GetRequest("ShopID");
        hDay.Value = Shove._Web.Utility.GetRequest("SaleDate");
    }

    /// <summary>
    /// 数据梆定
    /// </summary>
    private void BindData()
    {
        DataSet ds = null;

        long ShopID = Shove._Convert.StrToLong(this.hShopID.Value, 0);              //店铺ID   
        string Day = "";

        try
        {
            Day = Shove._Convert.StrToDateTime(this.hDay.Value, "1900-12-12").ToString("yyyy-MM-dd");
        }
        catch { } 

        try
        {
            //DAL.Procedures.P_Site_RevenueDetailList2(ref ds, ShopID, Day);
            this.BindDetail(ds);
        }
        catch { }
    }

    /// <summary>
    /// 梆定该单据的所有明细
    /// </summary>
    /// <param name="ds"></param>
    private void BindDetail(DataSet ds)
    {
        dt.Clear();
        dt = ds.Tables[0];

        UWGDetail.DataSource = dt;
        UWGDetail.DataBind();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }
}